package com.siderakis.gwt.cellwidgets.client;

import com.google.gwt.cell.client.AbstractEditableCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.siderakis.gwt.cellwidgets.client.colorpicker.ColorPickerDialog;
import com.siderakis.gwt.cellwidgets.client.dialog.DialogClosedEvent;
import com.siderakis.gwt.cellwidgets.client.dialog.IDialogClosedHandler;
import com.siderakis.gwt.cellwidgets.shared.HasColor;

/**
 * 
 * @author Nick Siderakis
 * 
 * */
public class ColorCell extends AbstractEditableCell<HasColor, HasColor> {

	public interface ColorCellSafeHTMLTemplate extends SafeHtmlTemplates {
		@Template("<div><div style=\"height:1.2em;width:100%;cursor:default;border:thin #000 solid;background:#{0};\"></div></div>")
		SafeHtml color(String color);
	}

	private static final ColorCellSafeHTMLTemplate TEMPLATE = GWT
	.create(ColorCellSafeHTMLTemplate.class);

	private final String color = "000000";
	private Object lastKey;

	private Element lastParent;
	private HasColor lastValue;
	private ValueUpdater<HasColor> valueUpdater;

	final ColorPickerDialog dlg = new ColorPickerDialog();

	private final int offsetX = 10;

	private final int offsetY = 10;

	public ColorCell() {
		super("click");
	}

	@Override
	public boolean isEditing(final Element parent, final HasColor value,
			final Object key) {
		return lastKey != null && lastKey.equals(key);
	}

	@Override
	public void onBrowserEvent(final Element parent, final HasColor value,
			final Object key, final NativeEvent event,
			final ValueUpdater<HasColor> valueUpdater) {

		super.onBrowserEvent(parent, value, key, event, valueUpdater);
		if ("click".equals(event.getType())) {
			onEnterKeyDown(parent, value, key, event, valueUpdater);
		}

	}

	@Override
	protected void onEnterKeyDown(final Element parent, final HasColor value,
			final Object key, final NativeEvent event,
			final ValueUpdater<HasColor> valueUpdater) {
		this.lastKey = key;
		this.lastParent = parent;
		this.lastValue = value;
		this.valueUpdater = valueUpdater;

		final HasColor viewData = getViewData(key);
		final HasColor color = (viewData == null) ? value : viewData;

		dlg.setColor(color.getColor());
		dlg.addDialogClosedHandler(new IDialogClosedHandler() {
			@Override
			public void dialogClosed(final DialogClosedEvent event) {
				if (!event.isCanceled()) {
					setColor(dlg.getColor());
				}
			}
		});
		dlg.setPopupPosition(parent.getAbsoluteLeft() + offsetX,
				parent.getAbsoluteTop() + offsetY);
		dlg.show();
	}

	@Override
	public void render(final HasColor value, final Object key,
			final SafeHtmlBuilder sb) {

		// Get the view data.
		HasColor viewData = getViewData(key);
		if (viewData != null && viewData.equals(value)) {
			clearViewData(key);
			viewData = null;
		}

		final String color = value != null && value.getColor() != null ? value
				.getColor() : "ffffff";

				sb.append(viewData != null && viewData.getColor() != null ? TEMPLATE
						.color(viewData.getColor()) : TEMPLATE.color(color));
	}

	private void setColor(final String color) {
		// Remember the values before hiding the popup.
		final Element cellParent = lastParent;
		final HasColor oldValue = lastValue;
		final Object key = lastKey;
		// panel.hide();

		// Update the cell and value updater.
		final HasColor newColor = new HasColor() {
			@Override
			public String getColor() {
				return color;
			}
		};
		setViewData(key, newColor);
		setValue(cellParent, oldValue, key);
		if (valueUpdater != null) {
			valueUpdater.update(newColor);
		}
	}

}
